package com.dormitory.ui;

import com.dormitory.model.Building;
import com.dormitory.model.Room;
import com.dormitory.service.BuildingService;
import com.dormitory.service.impl.BuildingServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class RoomFormDialog extends JDialog {
    private JComboBox<Building> buildingComboBox;
    private JTextField roomNumberField;
    private JTextField capacityField;
    private JTextField occupancyField;
    private JComboBox<String> statusComboBox;
    private boolean confirmed = false;
    private Room room;
    private BuildingService buildingService = new BuildingServiceImpl();

    public RoomFormDialog(Window owner, Room room) {
        super(owner, "房间信息", ModalityType.APPLICATION_MODAL);
        this.room = room;
        initUI();
        if (room != null) {
            fillForm(room);
        }
        pack();
        setLocationRelativeTo(owner);
    }

    private void initUI() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 所属楼栋
        gbc.gridx = 0; gbc.gridy = 0;
        panel.add(new JLabel("所属楼栋："), gbc);
        gbc.gridx = 1;
        buildingComboBox = new JComboBox<>();
        loadBuildings();
        panel.add(buildingComboBox, gbc);

        // 房间号
        gbc.gridx = 0; gbc.gridy = 1;
        panel.add(new JLabel("房间号："), gbc);
        gbc.gridx = 1;
        roomNumberField = new JTextField(15);
        panel.add(roomNumberField, gbc);

        // 容量
        gbc.gridx = 0; gbc.gridy = 2;
        panel.add(new JLabel("容量："), gbc);
        gbc.gridx = 1;
        capacityField = new JTextField(15);
        panel.add(capacityField, gbc);

        // 当前人数
        gbc.gridx = 0; gbc.gridy = 3;
        panel.add(new JLabel("当前人数："), gbc);
        gbc.gridx = 1;
        occupancyField = new JTextField(15);
        panel.add(occupancyField, gbc);

        // 状态
        gbc.gridx = 0; gbc.gridy = 4;
        panel.add(new JLabel("状态："), gbc);
        gbc.gridx = 1;
        statusComboBox = new JComboBox<>(new String[]{"空闲", "已满", "维修中"});
        panel.add(statusComboBox, gbc);

        // 按钮
        JPanel btnPanel = new JPanel();
        JButton okBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");
        okBtn.addActionListener(e -> onOK());
        cancelBtn.addActionListener(e -> onCancel());
        btnPanel.add(okBtn);
        btnPanel.add(cancelBtn);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(btnPanel, BorderLayout.SOUTH);
    }

    private void loadBuildings() {
        buildingComboBox.removeAllItems();
        List<Building> buildings = buildingService.getAllBuildings();
        for (Building building : buildings) {
            buildingComboBox.addItem(building);
        }
    }

    private void fillForm(Room r) {
        // 选中所属楼栋
        for (int i = 0; i < buildingComboBox.getItemCount(); i++) {
            if (buildingComboBox.getItemAt(i).getBuildingId().equals(r.getBuildingId())) {
                buildingComboBox.setSelectedIndex(i);
                break;
            }
        }
        roomNumberField.setText(r.getRoomNumber());
        capacityField.setText(String.valueOf(r.getCapacity()));
        occupancyField.setText(String.valueOf(r.getCurrentOccupancy()));
        statusComboBox.setSelectedItem(r.getRoomStatus());
    }

    private void onOK() {
        Building selectedBuilding = (Building) buildingComboBox.getSelectedItem();
        if (selectedBuilding == null) {
            JOptionPane.showMessageDialog(this, "请选择所属楼栋！");
            return;
        }
        if (roomNumberField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "房间号不能为空！");
            return;
        }
        int capacity, occupancy;
        try {
            capacity = Integer.parseInt(capacityField.getText().trim());
            if (capacity < 1 || capacity > 20) {
                JOptionPane.showMessageDialog(this, "容量应在1-20之间！");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "容量必须为数字！");
            return;
        }
        try {
            occupancy = Integer.parseInt(occupancyField.getText().trim());
            if (occupancy < 0 || occupancy > capacity) {
                JOptionPane.showMessageDialog(this, "当前人数不能小于0且不能大于容量！");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "当前人数必须为数字！");
            return;
        }
        confirmed = true;
        if (room == null) room = new Room();
        room.setBuildingId(selectedBuilding.getBuildingId());
        room.setRoomNumber(roomNumberField.getText().trim());
        room.setCapacity(capacity);
        room.setCurrentOccupancy(occupancy);
        room.setRoomStatus((String) statusComboBox.getSelectedItem());
        dispose();
    }

    private void onCancel() {
        confirmed = false;
        dispose();
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public Room getRoom() {
        return room;
    }
} 